package com.fh.shop.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/12/1315:42
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.exceptions.ParamException;
import com.fh.shop.entity.po.Imgs;
import com.fh.shop.entity.po.ShopBean;
import com.fh.shop.entity.po.ShopValues;
import com.fh.shop.entity.vo.ShopVo;
import com.fh.shop.mapper.ShopMapper;
import com.fh.shop.mapper.ShopValuesMapper;
import com.fh.shop.service.ImgsService;
import com.fh.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;
import java.util.*;

/**
 * created by macro on 2021/12/13,
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;

    @Autowired
    private ImgsService imgsService;

    @Autowired
    private ShopValuesMapper shopValuesMapper;


    @Override
    public List<ShopBean> qeury() {
        return shopMapper.qeury();
    }



    @Override
    public Map queryPage(ShopVo shopVo) {
        if (shopVo.getCurrent() == null || shopVo.getPageSize()==null) {
            throw new ParamException();
        }
        Map map = new HashMap();
        Page<ShopBean> page = new Page<>(shopVo.getCurrent(), shopVo.getPageSize());

        if (shopVo.getTypeId() == null && shopVo.getBrandId()==null) {
            IPage iPage2 = shopMapper.selectPage(page, null);
            List<ShopBean> shop2 = iPage2.getRecords();
            map.put("data", shop2);
            map.put("total", iPage2.getTotal());
        } else if (shopVo.getTypeId() == null){
            QueryWrapper q = new QueryWrapper();
            q.eq("brandId", shopVo.getBrandId());
            IPage iPage2 = shopMapper.selectPage(page, q);
            List<ShopBean> shop2 = iPage2.getRecords();
            map.put("data", shop2);
            map.put("total", iPage2.getTotal());
        } else if (shopVo.getBrandId() == null) {

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("typeId", shopVo.getTypeId());
            IPage iPage = shopMapper.selectPage(page, queryWrapper);
            List<ShopBean> shop = iPage.getRecords();
            map.put("data", shop);
            map.put("total", iPage.getTotal());
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("typeId", shopVo.getTypeId());
            queryWrapper.eq("brandId", shopVo.getBrandId());
            IPage iPage = shopMapper.selectPage(page, queryWrapper);
            List<ShopBean> shop = iPage.getRecords();
            map.put("data", shop);
            map.put("total", iPage.getTotal());
        }

      /*  *//*  构建返回数据 *//*
        TableData<Shop> rs=new TableData<>();
        *//* 验证参数 *//*
        if(params.getPage()==null||params.getSize()==null){
            throw new ParamException();
        }

        QueryWrapper<Shop> where=new QueryWrapper<>();
        if(params.getTypeId()!=null){
            where.eq("typeid",params.getTypeId());
        }
        if(params.getBrandId()!=null){
            where.eq("brandId",params.getBrandId());
        }
        *//*  mybatisplus 怎么做 分页条件查询 *//*
        *//* 构建分页对象 *//*               //page 当前页数      size  展示几条
        IPage<Shop> page=new Page<>(params.getPage(),params.getSize());
        *//* 得到分页数据 *//*
        IPage<Shop> shopIPage = shopDao.selectPage(page, where);
        rs.setCount(shopIPage.getTotal());
        rs.setRows(shopIPage.getRecords());
        return rs;*/

        return map;
    }

    @Override
    public ShopBean qeuryById(Integer id) {
        return shopMapper.selectById(id);
    }

    @Override
    public Map qeuryByIdAllData(Integer id) {
        Map map = new HashMap();

        ShopBean shopBean = shopMapper.selectById(id);
        List<Imgs> imgs = imgsService.queryById(id);
        List a = new ArrayList();
        a.add(shopBean.getImg());
        for (Imgs s : imgs) {
            a.add(s.getImg());
        }
        List<List> setMeal=new ArrayList<>();

        List l = new ArrayList();
        List list = new ArrayList();
        int count = 0;
        QueryWrapper<ShopValues> q = new QueryWrapper();
        q.eq("pid", id);
        List<ShopValues> shopValues = shopValuesMapper.selectList(q);
        for (int i = 0; i < shopValues.size(); i++) {
            ShopValues sv = shopValues.get(i);
            JSONObject js = (JSONObject) JSONObject.parse(sv.getValuess());
            if (js.getDouble("price") != null) {
                count++;
                Map m = new HashMap();
                m.put("id", sv.getId());
                m.put("price", js.getDouble("price"));
                StringBuffer sb = new StringBuffer();
                Set<String> keySet = js.keySet();
                for (String key :keySet) {
                    if (key.equals("price") || key.equals("count")) {
                        continue;
                    }
                    sb.append(shopMapper.queryName(key, js.getString(key))).append("-");
                }
                m.put("intro", sb.substring(0, sb.length() - 1));
                l.add(m);
                if (count % 3 == 0) {
                    setMeal.add(l);
                }
            }else {
                Map b = new HashMap();
                Set<String> keySet = js.keySet();
                for (String key :keySet) {
                    b.put("title", shopMapper.queryKeyName(key));
                    b.put("content", shopMapper.queryName(key, js.getString(key)));
                    list.add(b);
                }

            }
        }
        if (count % 3 != 0) {
            setMeal.add(l);
        }
        map.put("shop", shopBean);
        map.put("imgs", a);
        map.put("setMeal", setMeal);
        map.put("param", list);
        return map;
    }

}
